package com.zhongwang.mms.module.tool.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.module.tool.model.AccEquipmentTypeModel;
import com.zhongwang.mms.module.tool.service.AccEquipmentTypeService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 设备台账类型
 * @Author: 闫薇
 */
@Controller
public class AccEquipmentTypeController extends BaseController {

    @Autowired
    private AccEquipmentTypeService accEquipmentTypeService;

    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/tool/equipmentType/page")
    @RequiresPermissions({"basic:acctype:view"})
    public String linkePage() {
        return "tool/equipmentType/search";
    }

    /**
     * 设备台账类型列表查询
     * @param param
     * @return
     */
    @PostMapping("/tool/equipmentType/sel")
    @ResponseBody
    @RequiresPermissions({"basic:acctype:view"})
    public JsonResult selectAccEquipmentTypeList(AccEquipmentTypeModel param) {
        startPage();
        List<AccEquipmentTypeModel> accEquipmentTypeModelList = accEquipmentTypeService.selectAccEquipmentTypeList(param);
        return jsonResult(accEquipmentTypeModelList);
    }

    /**
     * 删除
     * @param typId 主键id
     * @return
     */
    @PostMapping("/tool/equipmentType/del")
    @ResponseBody
    @RequiresPermissions({"basic:acctype:edit"})
    public JsonResult deleteByPrimaryKey(Integer typId){
        JsonResult jsonResult = new JsonResult();
        AccEquipmentTypeModel record=new AccEquipmentTypeModel();
        record.setTypId(typId);
        record.setTypIsdelete(1);
        record.setTypUpdUser(ShiroUtils.getLoginName());
        int flg=accEquipmentTypeService.updateByPrimaryKey(record);
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    /**
     * 新增设备台账类型
     * @param record
     * @return
     */
    @PostMapping("/tool/equipmentType/add")
    @ResponseBody
    @RequiresPermissions({"basic:acctype:edit"})
    public JsonResult insert(AccEquipmentTypeModel record){
        JsonResult jsonResult = new JsonResult();
        record.setTypIsdelete(0);
        record.setTypCreUser(ShiroUtils.getLoginName());
        record.setTypUpdUser(ShiroUtils.getLoginName());
        int flg=accEquipmentTypeService.insert(record);
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    /**
     * 修改设备台账类型
     * @param record
     * @return
     */
    @PostMapping("/tool/equipmentType/edit")
    @ResponseBody
    @RequiresPermissions({"basic:acctype:edit"})
    public JsonResult  updateByPrimaryKey(AccEquipmentTypeModel record){
        JsonResult jsonResult = new JsonResult();
        record.setTypUpdUser(ShiroUtils.getLoginName());
        int flg=accEquipmentTypeService.updateByPrimaryKey(record);
        if (flg==1) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("修改成功");
        }
        else{
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }
    /**
     * 检查设备台账类型是否存在
     * @param paramModel 类型名称和id
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/tool/equipmentType/checkUnique")
    public ValidResult checkUnique(AccEquipmentTypeModel paramModel) {
        List<AccEquipmentTypeModel> list = accEquipmentTypeService.findByTypeName(paramModel);
        if(list.size()>0) {
            //已经存在了。
            return ValidResult.FAIL;
        }
        return ValidResult.SUCCESS;
    }
}
